/*
 * Configurator.h
 *
 *  Created on: May 13, 2014
 *      Author: alexander
 */

#ifndef CONFIGURATOR_H_
#define CONFIGURATOR_H_

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <map>

#include "FlightDatabase.h"
#include "Downloader.h"
#include "Configurator.h"

#include "Poco/Timestamp.h"
#include "Poco/String.h"
#include "Poco/StringTokenizer.h"
#include "Poco/NumberParser.h"
#include <ctime>

class Configurator
{
public:
	static void configure(std::map<std::string, std::string>& airports, std::vector<std::string>& flights)
	{
		// config
		std::ifstream cfgFile(CFG_FILE_NAME);
		if (cfgFile.is_open())
		{
			std::string str;
			while (std::getline(cfgFile, str))
			{
				Poco::StringTokenizer t1(str, "#",
						Poco::StringTokenizer::TOK_TRIM);
				if (!t1.count() || t1[0].size() <= 0)
				{
					continue;
				}

				Poco::StringTokenizer t2(t1[0], ":",
						Poco::StringTokenizer::TOK_IGNORE_EMPTY | Poco::StringTokenizer::TOK_TRIM);
				if (t2.count() != 2 || t2[0].size() <= 0 || t2[1].size() <= 0)
				{
					continue;
				}

				if (Poco::toUpper(t2[0]).compare("PROXY-ADDR") == 0)
				{
					proxyAddr = t2[1];
				}
				else if (Poco::toUpper(t2[0]).compare("PROXY-PORT") == 0)
				{
					proxyPort = Poco::NumberParser::parse(t2[1]);
				}
				else
				{
					std::cout << "Unrecognized config line: " << t1[0] << std::endl;
				}
			}
			cfgFile.close();
		}
		else
		{
			std::cout << "Unable to open: " << CFG_FILE_NAME << std::endl;
		}

		// airports
		std::ifstream airportFile(AIPORT_CODES_FILE);
		if (airportFile.is_open())
		{
			std::string str;
			while (std::getline(airportFile, str))
			{
				Poco::StringTokenizer t1(str, "#",
						Poco::StringTokenizer::TOK_TRIM);
				if (!t1.count() || t1[0].size() <= 0)
				{
					continue;
				}

				Poco::StringTokenizer t2(t1[0], ":",
						Poco::StringTokenizer::TOK_IGNORE_EMPTY | Poco::StringTokenizer::TOK_TRIM);
				if (t2.count() != 2 || t2[0].size() <= 0 || t2[1].size() <= 0)
				{
					continue;
				}
				airports[t2[0]] = t2[1];
			}
			airportFile.close();
		}
		else
		{
			std::cout << "Unable to open: " << AIPORT_CODES_FILE << std::endl;
		}

		// flights
		std::ifstream flightFile(FLIGHTS_FILE);
		if (flightFile.is_open())
		{
			std::string str;
			while (std::getline(flightFile, str))
			{
				Poco::StringTokenizer t1(str, "#",
						Poco::StringTokenizer::TOK_TRIM);
				if (!t1.count() || t1[0].size() <= 0)
				{
					continue;
				}

				flights.push_back(t1[0]);
			}
			flightFile.close();
		}
		else
		{
			std::cout << "Unable to open: " << FLIGHTS_FILE << std::endl;
		}
	}

	static bool getProxyParams(std::string& address, int& port)
	{
		if (proxyAddr.size() == 0 || proxyPort < 0)
		{
			return false;
		}
		address = proxyAddr;
		port = proxyPort;
		return true;
	}

private:
	static const std::string CFG_FILE_NAME;
	static const std::string AIPORT_CODES_FILE;
	static const std::string FLIGHTS_FILE;

	static std::string proxyAddr;
	static int proxyPort;
};

const std::string Configurator::CFG_FILE_NAME = "config";
const std::string Configurator::AIPORT_CODES_FILE = "airports";
const std::string Configurator::FLIGHTS_FILE = "ryanair_flights";

std::string Configurator::proxyAddr = "";
int Configurator::proxyPort = -1;


#endif /* CONFIGURATOR_H_ */
